package com.lxq.monitor.controller;

import com.google.gson.JsonObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RefreshScope
public class DevController {

    @Value("${server.port}")
    private String servicePort;
    @Value("${spring.application.name}")
    private String serviceName;
    @Autowired
    DiscoveryClient discoveryClient;
    //@Value("${test}")
    String applicationName="FUCK";
    @Autowired
    ResourceLoader resourceLoader;
    @Value("${proper}")
    String proper;

    @GetMapping("/discover")
    public List<String> getServices(){
        return discoveryClient.getServices();
    }

    @GetMapping("/hello")
    public String hello(String say){
        var jsonStr=new JsonObject();
        jsonStr.addProperty("port",servicePort);
        jsonStr.addProperty("service-name",serviceName);
        jsonStr.addProperty("say",say);
        return jsonStr.toString();
    }

    @GetMapping("/hello2")
    public String hello2(){
        var jsonStr=new JsonObject();
        jsonStr.addProperty("code",200);
        jsonStr.addProperty("port",servicePort);
        jsonStr.addProperty("uuid",UUID.randomUUID().toString());
        return jsonStr.toString();
    }

    @GetMapping("/sleep_req")
    public String sleepRequest() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        var jsonStr=new JsonObject();
        jsonStr.addProperty("port",servicePort);
        jsonStr.addProperty("code","ok");
        return jsonStr.toString();
    }

    @GetMapping("/sleep_req_out")
    @HystrixCommand(fallbackMethod = "timeoutResolution",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String sleepRequestTimeout() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        var jsonStr=new JsonObject();
        jsonStr.addProperty("port",servicePort);
        jsonStr.addProperty("code","ok");
        return jsonStr.toString();
    }

    @GetMapping("/sleep_req_out_2")
    public String sleepRequestTimeoutForHystrix() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        var jsonStr=new JsonObject();
        jsonStr.addProperty("port",servicePort);
        jsonStr.addProperty("code","ok");
        return jsonStr.toString();
    }

    @GetMapping("/get_dev_app_name")
    public String getDevApplicationName(){
        return applicationName;
    }

    @GetMapping("/get_property")
    public String getPropertyFile() throws IOException {
        var propertyInput=resourceLoader.getResource("classpath:bootstrap.properties").getInputStream();
        var stringBuf=new StringBuilder();
        var bytes=new byte[1024];
        while (true){
            var len=propertyInput.read(bytes);
            if (len==-1)break;
            stringBuf.append(new String(bytes, StandardCharsets.UTF_8));
        }
        return stringBuf.toString();
    }

    @GetMapping("/get_proper")
    public String getProper(){
        return proper;
    }

    private String timeoutResolution(){
        return "<h1>警告："+servicePort+"服务器炸了</h1>";
    }
}
